package com.agileboot.admin.controller.bot;

import com.agileboot.admin.customize.aop.accessLog.AccessLog;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.enums.common.BusinessTypeEnum;
import com.agileboot.common.utils.poi.CustomExcelUtil;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.bot.botQuestion.BotQuestionApplicationService;
import com.agileboot.domain.bot.botQuestion.command.AddBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.command.UpdateBotQuestionCommand;
import com.agileboot.domain.bot.botQuestion.dto.BotQuestionDTO;
import com.agileboot.domain.bot.botQuestion.query.BotQuestionQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问答信息操作处理
 *
 * @author pyb
 */
@Tag(name = "问答API", description = "问答相关的增删查改")
@RestController
@RequestMapping("/bot/bot-question")
@Validated
@RequiredArgsConstructor
public class BotQuestionController extends BaseController {

    private final BotQuestionApplicationService botQuestionApplicationService;

    /**
     * 获取问答列表
     */
    @Operation(summary = "问答列表")
//    @PreAuthorize("@permission.has('system:botquestion:list')")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<BotQuestionDTO>> list(BotQuestionQuery query) {
        PageDTO<BotQuestionDTO> pageDTO = botQuestionApplicationService.getBotQuestionList(query);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 导出问答列表
     */
    @Operation(summary = "问答导出")
    @AccessLog(title = "问答管理", businessType = BusinessTypeEnum.EXPORT)
//    @PreAuthorize("@permission.has('system:botquestion:export')")
    @GetMapping("/excel")
    public void export(HttpServletResponse response, BotQuestionQuery query) {
        List<BotQuestionDTO> all = botQuestionApplicationService.getBotQuestionListAll(query);
        CustomExcelUtil.writeToResponse(all, BotQuestionDTO.class, response);
    }

    /**
     * 获取问答详情
     */
    @Operation(summary = "问答详情")
//    @PreAuthorize("@permission.has('system:botquestion:query')")
    @GetMapping("/{id}")
    public ResponseDTO<BotQuestionDTO> getInfo(@PathVariable Integer id) {
        BotQuestionDTO dto = botQuestionApplicationService.getBotQuestionInfo(id);
        return ResponseDTO.ok(dto);
    }

    /**
     * 新增问答
     */
    @Operation(summary = "添加问答")
//    @PreAuthorize("@permission.has('system:botquestion:add')")
    @AccessLog(title = "问答管理", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseDTO<Void> add(@RequestBody AddBotQuestionCommand command) {
        botQuestionApplicationService.addBotQuestion(command);
        return ResponseDTO.ok();
    }

    /**
     * 修改问答
     */
    @Operation(summary = "修改问答")
//    @PreAuthorize("@permission.has('system:botquestion:edit')")
    @AccessLog(title = "问答管理", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseDTO<Void> edit(@RequestBody UpdateBotQuestionCommand command) {
        botQuestionApplicationService.updateBotQuestion(command);
        return ResponseDTO.ok();
    }

    /**
     * 删除问答
     */
    @Operation(summary = "删除问答")
//    @PreAuthorize("@permission.has('system:botquestion:remove')")
    @AccessLog(title = "问答管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping
    public ResponseDTO<Void> remove(@RequestParam @NotNull @NotEmpty List<Integer> ids) {
        botQuestionApplicationService.deleteBotQuestion(new BulkOperationCommand<>(ids));
        return ResponseDTO.ok();
    }

    @GetMapping("/getAll")
    @ResponseBody
    public List<Map<String, String>> getAllQuestionsAndAnswersAsJson() {
        List<BotQuestionDTO> list = botQuestionApplicationService.getBotQuestionListAll(new BotQuestionQuery());

        List<Map<String, String>> result = new ArrayList<>();
        for (BotQuestionDTO qa : list) {
            Map<String, String> map = new HashMap<>();
            map.put("question", qa.getQuestion());
            map.put("answer", qa.getAnswer());
            result.add(map);
        }

        return result;
    }
}
